{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c8717d3c",
   "metadata": {},
   "source": [
    "# Onekey lung segmentation algorithm for 3D CT images\n",
    "\n",
    "A sophisticated algorithm for segmenting lung regions from 3D CT images, a crucial task in medical image analysis. The algorithm is designed to handle the complexities of medical imaging data, where accurately isolating lung tissue is essential for further diagnostic or research purposes.\n",
    "\n",
    "### Algorithm Workflow:\n",
    "\n",
    "1. **Data Preprocessing:**\n",
    "   The algorithm begins by loading the CT scan data and converting it into a format suitable for processing. This involves resampling the images to standardize the spatial resolution, ensuring that the subsequent analysis is consistent regardless of the original scan resolution.\n",
    "\n",
    "2. **Initial Binarization:**\n",
    "   The next step involves transforming the CT image into a binary mask. This mask distinguishes between potential lung tissue and other structures based on intensity thresholds, effectively isolating areas that are likely to represent lungs or air-filled spaces.\n",
    "\n",
    "3. **Refinement of the Binary Mask:**\n",
    "   The binary mask is then refined through a series of operations:\n",
    "   - **Connected Component Analysis:** The algorithm identifies and labels distinct regions in the mask. This helps in distinguishing between different anatomical structures, such as lungs, bronchi, and external air.\n",
    "   - **Exclusion of Non-Lung Areas:** The algorithm systematically excludes regions that are unlikely to be part of the lungs, such as areas connected to the corners of the image or regions with volumes that are too small or too large to represent lung tissue.\n",
    "   - **Morphological Refinement:** The mask is further refined by filling small holes within the lung regions and separating the left and right lungs if they are connected.\n",
    "\n",
    "4. **Final Segmentation:**\n",
    "   After the refinement process, the algorithm produces two separate masks for the left and right lungs. These masks are combined into a final output that accurately represents the lung regions within the 3D CT scan.\n",
    "\n",
    "5. **Output Generation:**\n",
    "   The final step involves converting the segmented lung masks back into an image format and saving them for further use. This allows the segmented lungs to be utilized in various downstream applications, such as disease detection, volumetric analysis, or 3D visualization.\n",
    "\n",
    "### Reference\n",
    "\n",
    "[1]. OnekeyAI-Platform. (2024). Onekey (Version 4.8.18). GitHub repository. https://github.com/OnekeyAI-Platform/onekey"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c3b10ffa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<iframe src=\"//player.bilibili.com/player.html?isOutside=true&aid=113028490068626&bvid=BV18isjeSE7y&cid=500001663142606&p=1\" scrolling=\"no\" border=\"0\" frameborder=\"no\" framespacing=\"0\" allowfullscreen=\"true\"  width=\"980\" height=\"551\"></iframe>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 获得视频教程\n",
    "from onekey_algo.custom.Manager import onekey_show\n",
    "onekey_show('Module3-全肺分割')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7e647f56",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from onekey_algo.mietb.segmentation.FullLungSegmentation import seg_full_lung\n",
    "\n",
    "root = r'C:\\Users\\onekey\\Desktop\\demo\\images'\n",
    "save_dir = os.path.join(root, '../masks')\n",
    "seg_full_lung(root, save_dir=save_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9bc64d44",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
